<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_member_mobile_do_login_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
	    //var_dump($_SESSION); exit();
	
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	
	    //var_dump($_POST); exit();
	    $mobile = init_user_data_key($_POST, 'mobile', 'str');
	    $password  = init_user_data_key($_POST, 'password', 'str');
	    $member_login_captcha  = init_user_data_key($_POST, 'member_login_captcha', 'str');
	
        if(empty($mobile)){
            $result['status']  = -1;
            $result['message'] = '请输入手机';
        }else{
            $pattern_mobile = '/^\d{11}$/';
            $match_count    = preg_match($pattern_mobile, $mobile);
            if($match_count < 1){
                $result['status']  = -2;
                $result['message'] = '手机格式错误';
            }
        }
         
        if($result['status'] == 0){
            if(empty($password)){
                $result['status']  = -3;
                $result['message'] = '请输入密码';
            }
        }
         
        if($result['status'] == 0){
            if(empty($member_login_captcha)){
                $result['status']  = -4;
                $result['message'] = '请输入验证码';
            }else{
                $action_data = wwy_action::$data;
                $session_member_login_captcha = $action_data['session_member_login_captcha'];
                //var_dump($session_member_login_captcha); exit();
                if(empty($session_member_login_captcha)){
                    $result['status']  = -400;
                    $result['message'] = '非法操作';
                }else{
                    if(strtolower($member_login_captcha) !== $session_member_login_captcha){
                        $result['status']  = -5;
                        $result['message'] = '验证码错误';
                    }
                }
            }
        }
        //var_dump($result); exit();
        //exit();
         
        if($result['status'] == 0){
            $model_foreground_member = wwy_resource_container::get_model_foreground_member();
            //var_dump($model_foreground_member); exit();
            $where   = array('mobile' => $mobile);
            $member  = $model_foreground_member->fetch_row($where);
            //var_dump($member); exit();
            if(empty($member)){
                $result['status']  = -6;
                $result['message'] = '手机或密码不正确';
            }else{
                $password = md5($password);
                if($password != $member['password']){
                    $result['status']  = -6;
                    $result['message'] = '手机或密码不正确';
                }else{
                    $time = time();
                    $member_info = array(
                        'id'              => $member['id'],
                        'last_login_time' => $time,
                        'last_login_ip'   => get_client_ip(),
                        'login_count'     => $member['login_count'] + 1,
                        'update_time'     => $time
                    );
                    $update_member_login_count_result = $model_foreground_member->update_member_login_count($member_info);
                    //var_dump($update_member_login_count_result); exit();
                     
                    if(!$update_member_login_count_result){
                        $result['status']  = -7;
                        $result['message'] = '登录失败';
                    }else{
                        $result['status']  = 1;
                        $result['message'] = '登录成功';
                         
                        $set_session_member_info = array(
                            'id' => $member['id'],
                            'mobile' => $member['mobile'],
                            'default_address_id' => $member['default_address_id'],
                        );
                        wwy_action::$data['set_session_member_info'] = $set_session_member_info;
                    }
                }
                 
            }
        }
	     
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}